﻿using DingVision.Infrastructure.Events;
using DingVision.Infrastructure.Models;
using NLog;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System.Linq;

namespace DingVision.ViewModels
{
    public class MainWindowViewModel : BindableBase
    {
        #region 变量
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private readonly IDialogService dialogService;
        private readonly IRegionManager regionManager;
        private readonly IEventAggregator eventAggregator;
        #endregion
        #region 属性绑定
        private string _title = "DingVision";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        private string messageStr = "";
        public string MessageStr
        {
            get { return messageStr; }
            set { SetProperty(ref messageStr, value); }
        }
        private bool dmcLink;
        public bool DmcLink
        {
            get { return dmcLink; }
            set { SetProperty(ref dmcLink, value); }
        }
        #endregion
        #region 方法绑定
        private DelegateCommand<object> menuCommand;
        public DelegateCommand<object> MenuCommand =>
            menuCommand ?? (menuCommand = new DelegateCommand<object>(ExecuteMenuCommand));
        private DelegateCommand appLoadedEventCommand;
        

        public DelegateCommand AppLoadedEventCommand =>
            appLoadedEventCommand ?? (appLoadedEventCommand = new DelegateCommand(ExecuteAppLoadedEventCommand));


        #region 方法绑定函数
        void ExecuteMenuCommand(object obj)
        {
            if (obj.ToString() == "6")
            {
                dialogService.ShowDialog("AboutDialog");
            }
            else
            {
                var region = regionManager.Regions["ContentRegion"];
                foreach (var view in region.Views)
                {
                    region.Deactivate(view);
                }
                try
                {
                    switch (obj.ToString())
                    {
                        case "0":
                            {
                                var view = (Home.Views.HomePage)region.Views.Where(t => t.GetType() == typeof(Home.Views.HomePage)).FirstOrDefault();
                                region.Activate(view);
                            }
                            break;
                        case "3":
                            {
                                var view = (Axis.Views.AxisPage)region.Views.Where(t => t.GetType() == typeof(Axis.Views.AxisPage)).FirstOrDefault();
                                region.Activate(view);
                            }
                            break;
                        case "5":
                            {
                                var view = (ForceTest.Views.PressureCurve)region.Views.Where(t => t.GetType() == typeof(ForceTest.Views.PressureCurve)).FirstOrDefault();
                                region.Activate(view);
                            }
                            break;
                        default:
                            break;
                    }
                }
                catch (System.Exception ex)
                {
                    addMessage(ex.Message);
                }
               
            }
        }
        void ExecuteAppLoadedEventCommand()
        {
            #region 加载初始页面
            var region = regionManager.Regions["ContentRegion"];
            foreach (var _view in region.Views)
            {
                region.Deactivate(_view);
            }
            var view = (Home.Views.HomePage)region.Views.Where(t => t.GetType() == typeof(Home.Views.HomePage)).FirstOrDefault();
            region.Activate(view);
            #endregion
            addMessage("软件加载完成");
            eventAggregator.GetEvent<ShellSendEvent>().Publish("Loaded");
        }
        #endregion
        #endregion
        #region 构造函数
        public MainWindowViewModel(IDialogService _dialogService, IRegionManager _regionManager, IEventAggregator _eventAggregator)
        {
            dialogService = _dialogService;
            regionManager = _regionManager;
            eventAggregator = _eventAggregator;
            eventAggregator.GetEvent<ShellReceiveEvent>().Subscribe(ShellReceiveEventReceived);
        }
        #endregion
        #region 自定义函数
        private void addMessage(string str)
        {
            string[] s = MessageStr.Split('\n');
            if (s.Length > 1000)
            {
                MessageStr = "";
            }
            if (MessageStr != "")
            {
                MessageStr += "\n";
            }
            MessageStr += System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + str;
        }
        private void ShellReceiveEventReceived(ShellReceiveItem obj)
        {
            switch (obj.ModuleName)
            {
                case "AxisModule":
                    switch (obj.Content)
                    {
                        case "连接":
                            DmcLink = true;
                            break;
                        case "断开":
                            DmcLink = false;
                            break;
                        default:
                            break;
                    }
                    break;                
                default:
                    break;
            }
        }
        #endregion
    }
}
